package com.ljx.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljx.Vo.LoginVo;
import com.ljx.Vo.Result;
import com.ljx.dao.DeptDao;
import com.ljx.entity.Dept;
import com.ljx.entity.User;
import com.ljx.service.DeptService;
import com.ljx.util.JwtUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author LJX
 * @Description: TODO
 * @Date: 2022/11/17 19:35
 * @Version 1.0
 */
@Service
public class DeptServiceImpl implements DeptService {
	@Resource
	private DeptDao deptDao;

	@Override
	public Result loginVerify(LoginVo loginVo) {
		System.out.println(loginVo+"!!!!!!!!!!");

		QueryWrapper<Dept> wrapper = new QueryWrapper<>();
		wrapper.eq("name", loginVo.getName());
		wrapper.eq("address", loginVo.getAddress());
		Dept dept = deptDao.selectOne(wrapper);

		if(loginVo.getName().equals(dept.getName())&&loginVo.getAddress().equals(dept.getAddress())){
			// 生成token
			Map<String, Object> map = new HashMap<>();
			map.put("name",loginVo.getName());
			map.put("address",loginVo.getAddress());
			String token = JwtUtil.createToken(map);
			return new Result(2000,"登录成功",token);
		}
		return new Result(2000,"登录失败",null);
	}

	@Override
	public Result findAllDept(int currentPage, int pageSize) {
		Page<Dept> page = new Page<>(currentPage, pageSize);
		Page<Dept> deptPage = deptDao.selectPage(page, null);
		return new Result(200,"查询成功",deptPage);
	}

	@Override
	public Result updateDeptById(Dept dept) {
		UpdateWrapper<Dept> wrapper = new UpdateWrapper<>();
//		wrapper.eq("name","李吉星");
		int row = deptDao.updateById(dept);
		return new Result(200,"更新成功",row);
	}

	@Override
	public Result insertOne(Dept dept) {
		int row = deptDao.insert(dept);
		return new Result(200,"插入成功",row);
	}

	@Override
	public Result deleteById(int id) {
		int row = deptDao.deleteById(id);
		return new Result(200,"删除成功",row);
	}
}
